﻿@model KL.Store.Web.Models.ProductVariantListModel
@using KL.Store.Core;
@using CaptchaMvc.HtmlHelpers;
@{
    Layout = "~/Views/Shared/_LayoutBasic.cshtml"; ;
    ViewBag.Title = "Edit Properties";
}
<h3>Ürün Variant Özelliklerini Düzenle</h3>
<hr />
<div class="form-group">
    <div class="col-xs-12 first">
        <div id="productVariants">
            <table class="table table-bordered" id="EditProductVariant">
                @foreach (var info in Model.VariantList)
                {
                    <tr>
                        <td style="width: 50%;">
                            <input type="text" data-type="SatirNo" data-id="@info.id" data-original="@info.order" value="@info.order" class="form-control Satir" />
                        </td>
                        <td style="width: 50%;">
                            <input type="text" data-type="Deger" data-id="@info.id" data-original="@info.value" value="@info.value" class="form-control Deger" />
                        </td>
                        <td>
                            <a href="javascript:void(0)" class="RemoveItem" data-id="@info.id"><i class="fa fa-remove fa-2x"></i></a>
                        </td>
                    </tr>
                }
            </table>
            <div class="form-group">
                <div class="col-xs-4 first">
                    <form class="form-inline" role="form">
                        <div class="form-group">
                            <input type="text" name="addDeger" id="addDeger" class="form-control" placeholder="Değer" />
                        </div>

                        <div class="form-group">
                            <button type="button" id="addSave" class="btn btn-custom1">Ekle</button>
                        </div>
                    </form>
                </div>
                <div class="col-xs-8 first alignright">
                    <div class="form-group col-xs-7 first last right">
                        @if (Model.UstUrunId > 0)
                        {
                            <button type="button" id="UstUrundenGetir" class="btn btn-custom1">Üst Üründen Getir</button>
                        }
                        <button type="button" id="kaydetHepsiniveKapat" class="btn btn-custom1">Kaydet ve Kapat</button>
                        <button type="button" id="kaydetHepsini" class="btn btn-custom1">Kaydet</button>
                    </div>
                    <div class="form-group col-xs-4 last right">
                        <label for="Description" class="col-xs-5 control-label first">Üst Ürün ID'si: </label>
                        <div class="col-xs-7 first last">
                            <input type="text" name="ustUrunId" id="ustUrunId" class="form-control" placeholder="Üst Ürün Id" value="@Model.UstUrunId" />
                        </div>
                    </div>
                </div>
            </div>
            <div id="piResult" style="display: none;"></div>
            <div class="loading"></div>
        </div>
    </div>
</div>
<script>

    $('button#kaydetHepsini').click(function ()
    {
        kaydetHepsini(false);
    });
    $('button#kaydetHepsiniveKapat').click(function ()
    {
        kaydetHepsini(true);
    });

    function kaydetHepsini(kapansin)
    {
        loadingMsg();
        var ustUrunId = $('input[name=ustUrunId]').val();
        if (ustUrunId == '')
        {
            ustUrunId = 0;
        }
        var refreshFields = false;
        $('#productVariants .loading').show();
        var alanlar = $('table#EditProductVariant tr');
        var alanSayisi = alanlar.length;
        //alert(alanSayisi);
        var alanSayaci = 0;
        alanlar.each(function ()
        {
            $('#piResult').html('');
            var DataVariantId = $(this).find('td input.Satir').data('id');
            var DataSira = $(this).find('td input.Satir').val();
            var DataDeger = $(this).find('td input.Deger').val();
            var DataProduct = '@(Model.ProductId)';

            //alert(DataType + " " + DataOriginal + " " + DataValue + " " + DataProduct);
            $('#piResult').load('/ProductManage/ProductVariantSave', { variantid: DataVariantId, sira: DataSira, deger: DataDeger, dataproduct: DataProduct }, function ()
            {
                alanSayaci++;
                if (alanSayaci == alanSayisi)
                {
                    $('#piResult').load('/ProductManage/ProductVariantSingleUpdate', { ProductId: '@(Model.ProductId)', UstUrunId: ustUrunId }, function ()
                    {

                        if (kapansin)
                        {
                            parent.$.fancybox.close();
                            parent.location.reload(true);
                        }
                        else
                        {
                            $('#EditVariant').load('/ProductManage/EditVariantGet', { id: '@(Model.ProductId)' }, function ()
                            {
                                $('#productVariants .loading').hide();
                                $('#processResult').show(0).html('<div class="bg-info"><p><i class="fa fa-info-circle"></i>&nbsp;Bilgiler Kaydedildi</p></div>').delay(5000).hide(0);
                                if ($.browser.webkit)
                                {
                                    $('body:not(:animated)').animate({ scrollTop: 0 }, 'slow');
                                }
                                else
                                {
                                    $('html:not(:animated)').animate({ scrollTop: 0 }, 'slow');
                                }
                            });
                        }
                    });
                }
            });
        });
        if (alanSayisi <= 0)
        {
            hideMsg();
            if (kapansin)
            {
                parent.$.fancybox.close();
                parent.location.reload(true);
            }
        }
    }

    $('button#UstUrundenGetir').click(function ()
    {
        if (confirm('Üst üründeki tüm değerler getirilecektir, emin misiniz?'))
        {
            loadingMsg();
            $('#piResult').load('/ProductManage/ProductVariantInsertFromMain', { MainProductId: '@(Model.UstUrunId)', SubProductId: '@(Model.ProductId)' }, function ()
            {
                $('#EditVariant').load('/ProductManage/EditVariantGet', { id: '@(Model.ProductId)' }, function ()
                {
                    hideMsg();
                });
            });
        }
    });

    $('.RemoveItem').click(function ()
    {
        if (confirm('Silmek istediğinizden emin misiniz?'))
        {
            loadingMsg();
            var DataVariantId = $(this).data('id');
            var DataProduct = '@(Model.ProductId)';
            $('#piResult').load('/ProductManage/ProductVariantRemove', { ProductId: DataProduct, VariantId: DataVariantId }, function ()
            {
                $('#EditVariant').load('/ProductManage/EditVariantGet', { id: '@(Model.ProductId)' }, function ()
                {
                    hideMsg();
                });
            });
        }
    });

    //ekle
    $('button#addSave').click(function ()
    {
        loadingMsg();
        var DataDeger = $('input#addDeger').val();
        var DataProduct = '@(Model.ProductId)';
        $('#piResult').load('/ProductManage/ProductVariantInsert', { ProductId: DataProduct, Deger: DataDeger }, function ()
        {
            $('input#addDeger').val('');
            $('#EditVariant').load('/ProductManage/EditVariantGet', { id: '@(Model.ProductId)' }, function ()
            {
                hideMsg();
            });
        });
    });

</script>
